<?php
/**
 * CreateTime: 2023/2/15 17:50
 */

namespace App\Tools\Sms;

use App\Func\AppFunc;
use App\Func\CurlFunc;
use App\Func\TokenFuc;
use App\Models\SystemLog\SmsSystemLog;
use App\Tools\USails\USailsClient;

class DbaicaiSms
{

    private string $userName = '894712';

    private string $password = 'jl92aaD1YtDR';

    private string $url = 'https://api.yizhanda.net/sms/';

    private static $dBaicaiSms;


    private function getSign($time){
        return md5($this->userName.$time.md5($this->password));
    }

    private function getContentSign($content,$time){
        return md5($this->userName.$content.$time.md5($this->password));
    }

    private function __construct(){}

    private function __clone(): void{}

    public static function getInstance(){
        if(!self::$dBaicaiSms){
            self::$dBaicaiSms = new DbaicaiSms();
        }
        return self::$dBaicaiSms;
    }

    public function changeRequestType($type){
        $this->requestType = $type;
    }

    public function execute($path,$data = []){
        $publicHeader = [
            'Content-type:application/json;charset=utf-8',
            'Accept:application/json'
        ];
        $url = $this->url.$path;
        $sslm = new SmsSystemLog();
        switch ($this->requestType){
            case "GET":
                $res = CurlFunc::CurlGet($url,1,$data,$publicHeader);
                $sslm->add([
                    'request_info' => json_encode($data,256),
                    'response_info' => $res,
                    'type_str' => '大白菜',
                    'request_header' => json_encode($publicHeader,256),
                    'owner_id' => 0
                ]);
                return $res;
            case "POST":
            default:
                $res = CurlFunc::CurlPost($url,$data,$publicHeader);
                $sslm->add([
                    'request_info' => json_encode($data,256),
                    'response_info' => $res,
                    'type_str' => '大白菜',
                    'request_header' => json_encode($publicHeader,256),
                    'owner_id' => 0
                ]);
                return $res;
        }
    }

    public function sendMsg($data){
        $time = $this->msectime();
        $jsonData["userName"]=$this->userName;
        $jsonData["messageList"]= $data;
        $jsonData["timestamp"]=$time;
        $jsonData["sign"]=$this->getSign($time);
        return $jsonData;
    }


    public function test(){
        $msg="";
        # 数组形式，每个 JSON 对象包含短信内容和号码数据，最大 1000 个号码。
        $messageList = [
            [
                'phone'=> "15828111021",
                'content'=> '【四川省企业经济促进会人民调解委员会】与政富惠普平台的政富惠普金融服务平台金融到份一案，我会已受理北京恒元信业信息技术有限公司的调解由请.'
            ],
            [
                'phone'=> "17748119841",
                'content'=> '【四川省企业经济促进会人民调解委员会】与政富惠普平台的政富惠普金融服务平台金融到份一案，我会已受理北京恒元信业信息技术有限公司的调解由请.'
            ]
        ];
        //时间戳
        $timestamp=$this->msectime();
        //sign鉴权
        $sign=md5($this->userName.strval($timestamp).md5($this->password));
        $jsonData=array();
        $jsonData["userName"]=$this->userName;
        $jsonData["messageList"]=$messageList;
        $jsonData["timestamp"]=$timestamp;
        $jsonData["sign"]=$sign;
        $jsons= json_encode($jsonData,256);
        $url='https://api.yizhanda.net/sms/api/sendMessageOne';
        return $this->post($url,$jsons);
    }


    public function getSMSTemp($tempId = 0){
        $time = $this->msectime();
        $data = [
            'userName' => $this->userName,
            'timestamp' => $time,
            'sign' => $this->getSign($time),
        ];
        if($tempId){
            $data['templateId'] = $tempId;
        }
        return $data;
    }

    /**
     * 上行回复
     * @return array
     */
    public function getUpstream(){
        $time = $this->msectime();
        $data = [
            'userName' => $this->userName,
            'timestamp' => $time,
            'sign' => $this->getSign($time),
        ];
        return $data;
    }


    public function getReport(){
        $time = $this->msectime();
        $data = [
            'userName' => $this->userName,
            'timestamp' => $time,
            'sign' => $this->getSign($time),
        ];
        return $data;
    }

    public function addSMSTemp($content){
        $time = $this->msectime();
        $data = [
            'userName' => $this->userName,
            'timestamp' => $time,
            'sign' => $this->getContentSign($content,$time),
            'content' => $content,
        ];
        return $data;
    }


    /**
     * 返回当前的毫秒时间戳
     */
    private function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return strval($msectime);
    }


    private function post($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        echo "请求结果：$response\n";
    }

}